package com.mtt.erp.goods.controller;


import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.mtt.erp.common.annotation.Log;
import com.mtt.erp.common.api.R;
import com.mtt.erp.common.api.ResultCode;
import com.mtt.erp.common.controller.BaseController;
import com.mtt.erp.common.domain.QueryRequest;
import com.mtt.erp.goods.dto.TCategoryCreateDto;
import com.mtt.erp.goods.dto.TCategoryDto;
import com.mtt.erp.goods.dto.TCategoryUpdateDto;
import com.mtt.erp.goods.service.ITCategoryService;
import com.mtt.erp.goods.vo.TCategoryTreeVo;
import com.mtt.erp.goods.vo.TCategoryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

/**
 * @Description: 分类表Controller
 * @Author: guomengzhou
 * @CreateDate: 2019/6/4 14:23
 * @Version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/category")
@Api(value = "商品类型模板", tags = "M003-商品库-商品类型模板接口")
public class TCategoryController {
    @Autowired
    private ITCategoryService itCategoryService;

    /**
     * 类别列表(树状)
     *
     * @author guomengzhou
     */
    @Log("分类表信息")
    @GetMapping
    @RequiresPermissions("category:view")
    @ApiOperation(value = "类型列表", notes = "根据条件查询类型列表,tCategoryDto", position = 1)
    public R<TCategoryTreeVo> categoryList(QueryRequest request, TCategoryDto tCategoryDto) {
        TCategoryTreeVo categoryVo = this.itCategoryService.findCategory(request, tCategoryDto);
       if(categoryVo.getTotal()>0){
           return R.data(categoryVo);
       }else{
           return R.fail(ResultCode.NO_CONTENT);
       }
    }

    /**
     * 添加分类
     *
     * @author guomengzhou
     */
    @Log("添加分类")
    @PostMapping
    @RequiresPermissions("category:add")
    @ApiOperation(value = "新增商品类型", notes = "将商品类型保存到数据库,tCategoryCreateDto", position = 2)
    public R addCategory(@Valid TCategoryCreateDto tCategoryCreateDto) {
        try {
            this.itCategoryService.createCategory(tCategoryCreateDto);
            return R.success(ResultCode.SUCCESS);
        } catch (Exception e) {
            return R.fail(ResultCode.FAILURE);
        }
    }

    /**
     * 修改分类
     *
     * @author guomengzhou
     */
     @Log("修改分类")
    @PutMapping
    @RequiresPermissions("category:update")
    @ApiOperation(value = "修改商品类型", notes = "根据类型id修改类型信息,tCategoryUpdateDto", position = 3)
    public R updateMaFile(@Valid TCategoryUpdateDto tCategoryUpdateDto) {
        try {
            this.itCategoryService.updateCategory(tCategoryUpdateDto);
            return R.success(ResultCode.SUCCESS);
        } catch (Exception e) {
            return R.fail(ResultCode.FAILURE);
        }

    }

    /**
     * 删除分类(包括子分类)
     *
     * @author guomengzhou
     */
    @Log("删除分类")
    @DeleteMapping("/{categoryIds}")
    @RequiresPermissions("category:delete")
    @ApiOperation(value = "删除商品类型", notes = "批量删除商品类型,传入要删除的商品类型id串,以逗号分隔", position = 4)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "id串,以逗号分割", name = "categoryIds", required = true, paramType = "path")
    })
    public R deleteCategory(@NotBlank(message = "{required}") @PathVariable String categoryIds) {
        try {
            String[] ids = categoryIds.split(StringPool.COMMA);
            this.itCategoryService.deleteCategory(ids);
            return R.success(ResultCode.SUCCESS);
        } catch (Exception e) {
            return R.fail(ResultCode.FAILURE);
        }
    }
    /**
     * 商品类型对象
     *
     * @author guomengzhou
     */
    @Log("查询分类对象")
    @GetMapping("/{id}")
    @ApiOperation(value = "查询商品类型对象", notes = "根据类型id查询类型信息", position = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "商品类型id", required=true,paramType = "path")
    })
    public R<TCategoryVo> getCategory(@NotBlank(message = "{required}") @PathVariable String id) {
        TCategoryVo categoryVo = this.itCategoryService.findCategoryById(id);
        if (categoryVo != null) {
            return R.data(categoryVo);
        }else{
            return R.fail(ResultCode.NO_CONTENT);
        }
    }
}
